<template>
	<div class="pages">
		<tabTitle title="督办人信息"></tabTitle>
		<div class="backGround">
			<view class="item flex align-items ">
				<view class="item_left">督办人：</view>
				<view class="item_right  flex align-items">
					<u-input v-model="form.onusUserStr" placeholder="请选择" :fontSize="16" :readonly="true"
						placeholderClass="placeholderClass" style="pointer-events:none"></u-input>
					<u-icon name="arrow-right" color="#CCCCCC" :size="16"></u-icon>
				</view>
			</view>
			<view class="item flex align-items ">
				<view class="item_left">督办人部门：</view>
				<view class="item_right  flex align-items">
					<u-input v-model="form.deptStr" placeholder="请选择" :fontSize="16" :readonly="true"
						placeholderClass="placeholderClass" style="pointer-events:none"></u-input>
					<u-icon name="arrow-right" color="#CCCCCC" :size="16"></u-icon>
				</view>
			</view>
			<view class="item flex align-items " style="border: none;">
				<view class="item_left">下发时间：</view>
				<view class="item_right  flex align-items">
					<u-input v-model="form.issuedTime" placeholder="请选择" :fontSize="16" :readonly="true"
						placeholderClass="placeholderClass" style="pointer-events:none"></u-input>
					<u-icon name="arrow-right" color="#CCCCCC" :size="16"></u-icon>
				</view>
			</view>
		</div>
		<tabTitle title="督办信息"></tabTitle>
		<todoVue :form="form"></todoVue>
		<tabTitle title="任务下发"></tabTitle>
		<div class="backGround">
			<view class="item flex align-items ">
				<view class="item_left">下发数量 ：</view>
				<view class="item_right  flex align-items" @click="navTo">
					<u-input v-model="form.listVo.length" placeholder="请选择" :fontSize="16" :readonly="true"
						placeholderClass="placeholderClass" style="pointer-events:none"></u-input>
					<u-icon name="arrow-right" color="#CCCCCC" :size="16"></u-icon>
				</view>
			</view>
		</div>
		<u-button class="btn" @click="comit()" :loading="loading" loadingText="提交中">提交</u-button>
	</div>
</template>

<script>
	import tabTitle from '@/components/tabTitle.vue'
	import todoVue from './components/to-do.vue'
	import {
		getOperateUser
	} from '@/api/lssue.js'
	import {
		handleadd
	} from '@/api/supervise.js'
	export default {
		components: {
			tabTitle,
			todoVue
		},
		data() {
			return {
				form: {
					handleType: '', // 督办类型字段表key值
					handlePeriod: '', // 督办周期
					handleLevel: '', // 督办级别
					startDate: '', // 开始时间
					endDate: '', // 结束时间
					workTasks: '', // 工作任务
					keyTasks: '', // 重点任务
					requires: '', // 要求
					workContent: '', // 工作内容
					fileUrl: '', // 附件内容
					onusUser: '', // 责任人
					deptId: '', // 责任部门
					deptStr: '',
					issuedTime: '', // 下发时间
					listVo: [], // 下发人数组
					onusUserStr: '',
					handlePeriodStr: '', //督办周期文字
					handleLevelStr: '', //督办级别文字
				},
				loading: false
			}
		},
		onLoad() {
			if (this.$mp.query.token) {
				uni.setStorageSync('token', this.$mp.query.token)
			}
			this._getOperateUser()
			uni.removeStorageSync("list")

		},
		onShow() {
			uni.$once('list', e => {
				console.log(e, e);
				this.form.listVo = e
				uni.setStorageSync('list', JSON.stringify(e))
			})
		},
		methods: {
			// 获取当前登录人信息
			async _getOperateUser() {
				const res = await getOperateUser()
				console.log(res, "getuser");
				const userInfo = res.datas
				this.form.onusUser = userInfo.login_user_id
				this.form.onusUserStr = userInfo.login_user_name
				this.form.deptStr = userInfo.login_dept_name
				this.form.deptId = userInfo.login_dept_id
				this.form.issuedTime = this.getCurrentDateTime()
			},
			async comit() {
				console.log(this.form);
				if (this.hasEmptyValue(this.form, ['fileUrl'])) return uni.$u.toast('请将数据填写完整')
				let form = {
					...this.form
				}
				form.fileUrl = this.form.fileUrl ? JSON.stringify([this.form.fileUrl]) : ''
				try {
					handleadd(form).then(res => {
						console.log(res);
						uni.$u.toast('操作成功')
						setTimeout(() => {
							uni.navigateBack()
						}, 300)
					})

				} catch (err) {
					uni.$u.toast(err)
				}

			},
			hasEmptyValue(obj, excludeKeys = []) {
				if (typeof obj !== 'object' || obj === null) {
					return false; // 如果不是对象类型或者是 null，则不处理
				}

				// 遍历对象的所有属性
				for (let key in obj) {
					if (obj.hasOwnProperty(key)) {
						// 如果该属性在排除列表中，则跳过验证
						if (excludeKeys.includes(key)) {
							continue;
						}
						const value = obj[key];
						// 判断空值的条件
						if (
							value === null ||
							value === undefined ||
							value === "" ||
							Number.isNaN(value) ||
							(Array.isArray(value) && value.length === 0) ||
							(typeof value === "object" && Object.keys(value).length === 0) ||
							(typeof value === "object" && this.hasEmptyValue(value, excludeKeys)) // 递归检查嵌套对象
						) {
							return true; // 找到空值，返回 true
						}
					}
				}
				return false; // 如果没有空值，返回 false
			},
			navTo() {
				console.log(this.form, "111111111");
				if (!this.form.endDate && !this.form.startDate) return uni.$u.toast('请先选择开始时间和截至时间')
				this.jumpTo('/pages/supervise/task', {
					startDate: this.form.startDate,
					endDate: this.form.endDate
				})
			}

		}
	}
</script>
<style>
	page {
		background-color: #F6F6F6;
	}
</style>
<style lang="scss" scoped>
	.item {
		min-height: 100rpx;
		border-bottom: 1rpx solid #E6E6E6;
		padding: 0 31rpx;

		&_left {
			// width: 220rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 33rpx;
			line-height: 47rpx;
			text-align: left;
			font-style: normal;
			color: #666666;
		}

		&_left::before {
			content: '*';
			color: #FF0000;
		}

		&_right {
			flex: 1;

			/deep/.u-input {
				height: 65rpx;
				margin: 0 auto;
				font-size: 16rpx;
			}
		}

		&_fu {
			width: 196rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 33rpx;
			line-height: 47rpx;
			text-align: left;
			font-style: normal;
			color: #666666;
		}

		&_upload {
			width: 240rpx;
			height: 60rpx;
			background: #267DFF;
			text-align: center;
			line-height: 60rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 29rpx;
			color: #FFFFFF;
		}

	}

	.backGround {
		background-color: #FFF;
		padding-bottom: 5rpx;
	}

	.btn {
		background: #267DFF;
		width: 90%;
		height: 89rpx;
		text-align: center;
		line-height: 89rpx;
		font-family: PingFangSC, PingFang SC;
		font-weight: 400;
		font-size: 29rpx;
		color: #FFFFFF;
		margin: 0 auto;
		margin-top: 30rpx;
	}
</style>